package com.catmiw.stock.tool;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.catmiw.funds.tool.DateTool;
import com.catmiw.stock.domain.CapitalChange;
import com.catmiw.stock.domain.DayData;
import com.catmiw.stock.domain.Incoming;
import com.catmiw.stock.domain.Stock;
import com.catmiw.stock.domain.StockInfo;
import com.catmiw.stock.domain.StockPrice;
import com.catmiw.stock.domain.Stockdb;
import com.catmiw.stock.service.CapitalService;
import com.catmiw.stock.service.CapitalServiceImpl;
import com.catmiw.stock.service.EarningsService;
import com.catmiw.stock.service.EarningsServiceImpl;
import com.catmiw.stock.service.IncomingService;
import com.catmiw.stock.service.IncomingServiceImpl;
import com.catmiw.stock.service.StockPriceService;
import com.catmiw.stock.service.StockPriceServiceImpl;
import com.catmiw.stock.service.StockService;
import com.catmiw.stock.service.StockServiceImpl;

public class StockDatabase {
	public static void updateStock(){
		List<Stock> stocks = Stock.getStockList("SHSZ", "A");
		List<Stockdb> sdb=new ArrayList<Stockdb>();
		
		StockService is = new StockServiceImpl();
		is.deleteAll();
	
		for (Stock s : stocks) {
			s.loadMarketDataFromDiaryFile();
			s.setMarketdate(DateTool.toDate(s.getStockInfo().getMarketDay()));
			Stockdb sd=new Stockdb();
			sd.setCode(s.getCode());
			sd.setName(s.getName());
			sd.setMarket(s.getMarket());
			sd.setMarketdate(s.getMarketdate());
			sd.setType(s.getType());
			sdb.add(sd);
		}
		is.insertAll(sdb);
	}
	
	public static void updateIncoming(){
		List<Stock> stocks = Stock.getStockList("SHSZ", "A");
		IncomingService is = new IncomingServiceImpl();
		//is.deleteAll();
		//is.deleteAllDetail();
		
		for (Stock s : stocks) {
			s.loadMarketDataFromDiaryFile();
			s.loadIncomingData();
			is.insertAll(s.getIncomings());
			List<Incoming> list=s.getIncomings();
			for (Incoming ic:list){
				if (ic.getAreaIncomings()!=null&&ic.getAreaIncomings().size()>0){
					is.insertAllDetail(ic.getAreaIncomings());
				}
				if (ic.getIndustryIncomings()!=null&&ic.getIndustryIncomings().size()>0){
					is.insertAllDetail(ic.getIndustryIncomings());
				}
				if (ic.getProductIncomings()!=null&&ic.getProductIncomings().size()>0){
					is.insertAllDetail(ic.getProductIncomings());
				}
			}
		}
	}
	
	public static void updateCapital(){
		List<Stock> stocks = Stock.getStockList("SHSZ", "A");
		CapitalService is = new CapitalServiceImpl();
		is.deleteAll();
		for (Stock s : stocks) {
			s.loadCaptialChangeData();
			is.insertAll(s.getCapChanges());
		}
	}
	
	public static void updateEarnings(){
		List<Stock> stocks = Stock.getStockList("SHSZ", "A");
		EarningsService is = new EarningsServiceImpl();
		CapitalService isc = new CapitalServiceImpl();
		is.deleteAll();
		for (Stock s : stocks) {
			List<CapitalChange> lcc=isc.selectByCode(s.getCode());
			s.loadEarningData(lcc);
			is.insertAll(s.getEarnings());
		}
	}
	
	public static void updatePrice(Date date){
		List<Stock> stocks = Stock.getStockList("SHSZ", "A");
		StockPriceService is = new StockPriceServiceImpl();
		//is.deleteAll();
		List<StockPrice> lsp=new ArrayList<StockPrice>();
		for (Stock s : stocks) {
			System.out.println(s.getCode()+"\t"+s.getName());
			s.loadMarketDataFromDiaryFile();
			//StockInfo si=new StockInfo();
			//si.setMarketDay(20100101);
			//s.setStockInfo(si);
			
			DayData dd=s.loadDiaryData(date, false);
			StockPrice sp=new StockPrice();
			sp.setCode(s.getCode());
			sp.setDate(date);
			if (dd!=null)
				sp.setClose(dd.getClose());
			lsp.add(sp);
		}
		is.insertAll(lsp);
	}
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		updatePrice(DateTool.toDate(20081104));
		//updateStock();
		//System.out.println(DateTool.toDate("2010-6-30"));

	}

}
